package com.example.redisdemo.controller;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * Created by bruce on 2022/11/13.
 */
@RestController
public class ProviderController {
    @GetMapping("/produce")
    public String produce(){
        DefaultMQProducer mqProducer = new DefaultMQProducer("day02");
        mqProducer.setNamesrvAddr("127.0.0.1:9876");
        try {
            mqProducer.start();
            for(int i = 1; i <= 10; i++){
                Message message = new Message("mqtest",("Message" + i).getBytes(StandardCharsets.UTF_8));
                SendResult sendResult = mqProducer.send(message);
                System.out.println("producing message" + i);
            }
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        }

        mqProducer.shutdown();
        return "produce 10 records successfully!";
    }

}
